package com.lanju.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lanju.common.SpringUtils;
import com.lanju.dao.HouseTransferListDao;
import com.lanju.dao.UserDao;
import com.lanju.entity.Agreement;
import com.lanju.entity.HouseTransferList;
import com.lanju.entity.HouseTransferListItem;
import com.lanju.entity.User;
import com.lanju.model.ResultModel;
import com.lanju.service.HouseTransferListService;
import com.lanju.service.JedisService;

@Controller
@RequestMapping("/houseTransferList")
public class HouseTransferListController {
	private static final Logger LOGGER = LoggerFactory.getLogger(HouseTransferListController.class);
    
    @Autowired
    HouseTransferListService houseTransferListService;
    
    @Resource
   	JedisService jedisService;
    
    @Autowired
    HouseTransferListDao HouseTransferListDao;
    
    @Autowired
    UserDao userDao;
    
    @RequestMapping("/add")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultModel add(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		HouseTransferList entity = json.toJavaObject(HouseTransferList.class);
		entity.setUserId(param.get("userId").toString());
		entity.setCreateTime(new Date());

		String listItemArrStr = (String)param.get("listItemArrStr");
		JSONArray jsonArr = JSONArray.parseArray(listItemArrStr);
		Set<HouseTransferListItem> houseTransferListItemSet = new HashSet<HouseTransferListItem>() ;
		for( Object item : jsonArr){
			houseTransferListItemSet.add(((JSONObject) item).toJavaObject(HouseTransferListItem.class));
		}
		entity.setHouseTransferListItemSet(houseTransferListItemSet);
        return  houseTransferListService.add(entity);
    }
    
    
    @RequestMapping("/del")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultModel del(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		HouseTransferList entity = json.toJavaObject(HouseTransferList.class);
        return  houseTransferListService.del(entity);
    }
    
    @RequestMapping("delTransfer")
    @ResponseBody
    public Map<String,Object> delTransfer(HttpServletRequest request, HttpServletResponse response){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
    	JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
    	HouseTransferList entity = json.toJavaObject(HouseTransferList.class);
		return houseTransferListService.delTransfer(entity);
    }
    
    @RequestMapping("/edit")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultModel edit(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		HouseTransferList entity = json.toJavaObject(HouseTransferList.class);
		User user =(User) session.getAttribute("userModel");
		//entity.setUpdator(user.getUserId());
		//entity.setUpdateTime(new Date());
        return  houseTransferListService.edit(entity);
    }
    
    
    @RequestMapping("/getById")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Map getById(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		HouseTransferList entity = json.toJavaObject(HouseTransferList.class);
		HouseTransferList list = houseTransferListService.getById(entity.getTransferId());
		Map map = new HashMap<String,Object>();
		map.put("list", list);
		map.put("list_item",list==null?null:list.getHouseTransferListItemSet());
        return  map;
    }
    
    
    /** 根据参数获取 */
    @RequestMapping("/getByParam")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Map getByParam(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		HouseTransferList entity = json.toJavaObject(HouseTransferList.class);
		HouseTransferList list = houseTransferListService.getByParam(entity);
		Map map = new HashMap<String,Object>();
		map.put("list", list);
		if(list!=null){
			map.put("list_item",list==null?null:list.getHouseTransferListItemSet());
		}
        return  map;
    }
    
    public Map<String,Object> getTransfer(HttpServletRequest request, HttpServletResponse response){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
		return null;
    }
    
    /** 获取列表 */
    @RequestMapping("/getList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Map<String, Object> getList(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		if( "1".equals(param.get("userIdFlag")) ){
			User user =(User) session.getAttribute("userModel");
			param.put("userId", param.get("userId").toString());
		}
		Integer pageSize = Integer.parseInt( (String)param.get("pageSize") );
		Integer pageNumber = Integer.parseInt( (String)param.get("pageNumber") );
		Integer offset = (pageNumber-1)*pageSize;
        return  houseTransferListService.getList(param, pageSize, offset);
    }

    /** 获取房子列表 ( 不分页 ) */
    @RequestMapping("/getHouseList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public List<Map> getHouseList(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Agreement agreement = json.toJavaObject(Agreement.class);
        return  houseTransferListService.getHouseList(param);
    }
    
    /** 获取房子列表 ( 不分页 ) */
    @RequestMapping("/getBaseItemList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public List<Map> getBaseItemList(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Agreement agreement = json.toJavaObject(Agreement.class);
        return  houseTransferListService.getBaseItemList(param);
    }

}
